﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="Property-Link.aspx.cs" Inherits="PromotionEngine.Property_Link" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        var oCustomerPropertyLink = {};
        var oCustomer = {};
        var oCustomerPropertyDetail = {};
      var isInsert = false;
      var gird;
      function customerDropDownEditor(container, options) {
          $('<input required data-text-field="CustomerName" data-value-field="CustomerID" ID="CustomerID" data-bind="value:' + options.field + '"/>')
                        .appendTo(container)
                        .kendoDropDownList({
                            autoBind: false,
                            dataSource : oCustomer
                        });
                    }
        function customerPropertyDropDownEditor(container, options) {
            $('<input required data-text-field="PropertyDetailName" data-value-field="PropertyDetailID"  ID ="PropertyDetailID" data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoDropDownList({
                autoBind: false,
                dataSource: oCustomerPropertyDetail
            });
        }
       //Get All Customer
        function GetCustomers() {
            $.ajax({
                type: "GET",
                url: "Customer.aspx/GetCustomers",
                data: '',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    oCustomer = $.parseJSON(response.d);
                },
                failure: function (response) {
                    alert(response.d);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    if (jqXHR.status == 500) {
                        alert('Internal error: ' + jqXHR.responseText);
                    } else {
                        alert('Unexpected error.' + jqXHR.responseText);
                    }
                }
            });
        }
        //Get All Customer
        function GetCustomerPropertys() {
            $.ajax({
                type: "GET",
                url: "Customer-Property.aspx/GetAllCustomerPropertyDetails",
                data: '',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    oCustomerPropertyDetail = response.d.Data;

                },
                failure: function (response) {
                    alert(response.d);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    if (jqXHR.status == 500) {
                        alert('Internal error: ' + jqXHR.responseText);
                    } else {
                        alert('Unexpected error.' + jqXHR.responseText);
                    }
                }
            });
        }
        $(document).ready(function () {
            GetCustomers();
            GetCustomerPropertys();
            //Load Grid
            grid = $("#grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            type: "GET",
                            url: "Property-Link.aspx/GetCustomerPropertyLinks",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json"
                        },
                        serverPaging: true,
                        serverSorting: true,
                        serverFiltering: true
                    },
                    type: "odata", //Important!!!!
                    schema: {
                        data: "d.Data",
                        total: "d.Count",
                        model: {
                            id: "PropertyLinkID",
                            fields: {
                                PropertyLinkID: { type: "string" },
                                CustomerName: { type: "string" },
                                CustomerID: { type: "string", validation: { required: true} },
                                PropertyDetailName: { type: "string" },
                                PropertyDetailID: { type: "string", validation: { required: true} }
                            }
                        }
                    }
                },
                sortable: true,
                pageable: {
                    pageSize: 20,
                    refresh: true
                },
                filterable: true,
                reorderable: true,
                resizable: true,
                height: 450,
                toolbar: [{ name: 'create', text: 'Add New Property' }, { name: 'excel'}],
                excel: {
                    fileName: "Customer Property List.xlsx",
                    filterable: true
                },
                batch: true,
                columns: [
                            { field: "PropertyLinkID", title: "CompanyCode", width: 100, hidden: true },
                            { field: "CustomerID", title: "Customer", width: 300, editor: customerDropDownEditor, template: "#=CustomerName#" },
                            { field: "PropertyDetailID", title: "Property", editor: customerPropertyDropDownEditor, template: "#=PropertyDetailName#" },
                            { field: "CustomerName", title: "Customer Name", hidden: true },
                             { field: "PropertyDetailName", title: "Property Detail Name ", hidden: true },
                                 { command: ["edit", "destroy"], title: "Action", width: "270px" }
                            ],
                editable: {
                    mode: "inline",
                    confirmation: "Are you sure you want to remove this property?"
                },
                edit: function (e) {
                    if (e.model.isNew()) {
                        $("a.k-grid-update")[0].innerHTML = "<span class='k-icon k-update'></span>Save";
                        isInsert = true;
                    }
                    else {
                        isInsert = false;
                    }
                },
                remove: function (e) {
                },
                save: function (e) {
                    var listTextBox = $("input:text");
                    for (var i = 0; i < listTextBox.length; i++) {
                        var name = listTextBox[i].name;
                        if (name.length > 0) {
                            oCustomerPropertyLink[name] = $("[name=" + name + "]").val();
                        }
                    }
                    oCustomerPropertyLink.CustomerID = $('#CustomerID').data("kendoDropDownList").value();
                    oCustomerPropertyLink.PropertyDetailID = $('#PropertyDetailID').data("kendoDropDownList").value();
                    //Update or Insert Company
                    $.ajax({
                        type: "POST",
                        url: "Property-Link.aspx/UpdateCustomerPropertyLink",
                        data: JSON.stringify({
                            oCustomerPropertyLink: JSON.stringify(oCustomerPropertyLink),
                            isInsert: isInsert
                        }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            //Reload Data 
                            grid.dataSource.read();
                        },
                        error: function (xhr, status, err) {
                            var err = eval("(" + xhr.responseText + ")");
                            alert(err.Message);
                        }
                    });
                    this.refresh();
                }
            }).data("kendoGrid");
        });
    </script>
    <h2>
        Customer Property Link
    </h2>
                
                <h2>Property Code: #= CustomerCode # - Property Name: #= CustomerName #</h2>
     
    <div style="position: absolute; z-index: 0;">
        <div id="grid">
        </div>
    </div>
</asp:Content>
